package master.ghrm.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

@Entity
public class Note extends AbstractBaseEntity {

	private static final long serialVersionUID = 1L;

	public static final int LENGTH_CONTENT = 1000;
	private static final int LENGTH_CONTENT_SHORT = 40;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime created;

	@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
	private DateTime modified;

	@Column(length = LENGTH_CONTENT)
	private String content;

	private Boolean archived;

	@Transient
	private Boolean old = false;

	@Version
	private Long version;

	@ManyToOne
	@JoinColumn(name = "profile_id", nullable = false)
	@ForeignKey(name = "FK_NOTE_PROFILE")
	private Profile profile;

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param anId the id to set
	 */
	public void setId(Long anId) {
		this.id = anId;
	}

	/**
	 * @return the created date
	 */
	public DateTime getCreated() {
		return created;
	}

	/**
	 * @param timeCreated the created date to set
	 */
	public void setCreated(DateTime timeCreated) {
		this.created = timeCreated;
	}

	/**
	 * @return the modified date
	 */
	public DateTime getModified() {
		return modified;
	}

	/**
	 * @param timeModified the modified date
	 */
	public void setModified(DateTime timeModified) {
		this.modified = timeModified;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param aContent the content to set
	 */
	public void setContent(String aContent) {
		this.content = aContent;
	}

	/**
	 * @return the short content
	 */
	public String getContentShort() {
		String shortContent = "";
		if (content != null) {
			shortContent = content.replaceAll("\\<.*?>", "");
			if (shortContent.length() > LENGTH_CONTENT_SHORT) {
				shortContent = shortContent.substring(0, LENGTH_CONTENT_SHORT);
			}
		}

		return shortContent;
	}

	/**
	 * @return the archived
	 */
	public Boolean getArchived() {
		return archived;
	}

	/**
	 * @param isArchived the archived to set
	 */
	public void setArchived(Boolean isArchived) {
		this.archived = isArchived;
	}

	/**
	 * @param isOld the old to set
	 */
	public void setOld(Boolean isOld) {
		this.old = isOld;
	}

	/**
	 * @return the old
	 */
	public Boolean getOld() {
		return old;
	}

	/**
	 * For persistence use, only.
	 * 
	 * @return the version
	 */
	Long getVersion() {
		return version;
	}

	/**
	 * For persistence use, only.
	 * 
	 * @param aVersion the version to set
	 */
	void setVersion(Long aVersion) {
		this.version = aVersion;
	}

	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @param aProfile the profile to set
	 */
	public void setProfile(Profile aProfile) {
		this.profile = aProfile;
	}

	/**
	 * Exclude the profile from equals and hash code.
	 * 
	 * @see com.levi9.hrm.model.base.AbstractBaseEntity#getExcludedAttributes()
	 */
	@Override
	protected String[] getExcludedAttributes() {
		return new String[] { "profile" };
	}
}
